<?php
namespace Home\Controller;

class LoginController extends HomeController {

    public function index(){
        if (cookie('loginName')!='' && cookie('loginPassword')!='') {
            $loginName = \Think\Crypt\Driver\Xxtea::decrypt(cookie('loginName'),C('cookie_fix'));
            $loginPassword = \Think\Crypt\Driver\Xxtea::decrypt(cookie('loginPassword'),C('cookie_fix'));
            $this->assign('loginName',$loginName);
            $this->assign('loginPassword',$loginPassword);
        }
       	$this->display();
    }

    public function checklogin(){
    	$username = I('post.username');
        $password = I('post.password');
        $rememberme = I('post.rememberme');

        $obj = M('Member');

        if($username==''){
             $state = 'account required';
             echo $this->echo_json_str($state,$message,$url);die;
        }

        if($password==''){
            $state = 'password required';
            echo $this->echo_json_str($state,$message,$url);die;
        }
            
        $condition['username']=$username;

        $list = $obj->where($condition)->find();
        if($list) {
            if ($list['password']!=md5($password)) {
                $state = 'Invalid password';
                echo $this->echo_json_str($state,$message,$url);die;
            }  

            if ($list['status']==0) {
                $state = 'Account is not activated, please enter the registered mailbox for activation';                
                echo $this->echo_json_str($state,$message,$url);die;
            }  

            //登录成功
            $date['loginDate']=time();
            $date['loginIP']=get_client_ip();
            $obj->where($condition)->save($date);

            if ($rememberme=="forever") {  
                $exp = 3600*24*60;
                cookie('loginName',\Think\Crypt\Driver\Xxtea::encrypt($list['username'],C('cookie_fix')),$exp);
                cookie('loginPassword',\Think\Crypt\Driver\Xxtea::encrypt($password,C('cookie_fix')),$exp);
            }else{
                cookie('loginName',null);
                cookie('loginPassword',null);
            }

            /*cookie('userName',\Think\Crypt\Driver\Xxtea::encrypt($list['username'],C('cookie_fix')),$exp);
            cookie('userID',\Think\Crypt\Driver\Xxtea::encrypt($list['userid'],C('cookie_fix')),$exp);
            cookie('userType',\Think\Crypt\Driver\Xxtea::encrypt($list['userType'],C('cookie_fix')),$exp);*/

            cookie('userName',\Think\Crypt\Driver\Xxtea::encrypt($list['username'],C('cookie_fix')));
            cookie('userID',\Think\Crypt\Driver\Xxtea::encrypt($list['userid'],C('cookie_fix')));
            cookie('userType',\Think\Crypt\Driver\Xxtea::encrypt($list['userType'],C('cookie_fix')));
            cookie('userBasic',\Think\Crypt\Driver\Xxtea::encrypt($list['basic'],C('cookie_fix')));

            $state = 'SUCCESS';
            $message = 'login successful';
            if ($list['userType']==1) {
                $url = U('Mentees/index/index');
            }else{
                $url = U('Mentors/index/index');
            }    
            echo $this->echo_json_str($state,$message,$url);die;   
        }else{
            $state = 'This account does not exist';
            echo $this->echo_json_str($state,$message,$url);die;
        }
    }
}